package com.yzt.zhuju.permission;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.yzt.zhuju.R;
import com.yzt.zhuju.dialog.CenterBaseDialog;
import com.yzt.p_base.utils.AppInfoUtils;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 权限允许弹窗
 */
@SuppressLint("SetTextI18n")
public class PermissionTipDialog extends CenterBaseDialog implements DialogInterface.OnKeyListener {

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_permission_tip;
    }

    @Override
    public void bindView(View view) {
        String content = getArguments().getString("content");
        int type = getArguments().getInt("type", 0);
        TextView contentTv = view.findViewById(R.id.content_tv);
        contentTv.setText(getContext().getString(R.string.allow_or_not) + " Nomi to " + content);
        if (type == 1) {
            contentTv.setText(AppInfoUtils.getAppName(mActivity) + " " + content);
        }

        view.findViewById(R.id.cancel_tv).setOnClickListener(v -> {
            if (listener != null) {
                listener.cancel();
            }
            dismiss();
        });

        view.findViewById(R.id.submit_tv).setOnClickListener(v -> {
            if (listener != null) {
                listener.select();
            }
            dismiss();
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
    }

    private OnSelectListener listener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        return keyCode == KeyEvent.KEYCODE_BACK;
    }

    public interface OnSelectListener {

        void select();

        void cancel();
    }
}
